package com.mc.lp.database.entity;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;


@Entity(name = "T_ROLE")
public class RoleEntity extends MicroCampusBaseEntity {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Column(name = "role_name", nullable = false)
	private String roleName;
	
	@Column(name = "parent_role_id", nullable = false)
	private Long parentRoleId;

	@ManyToMany(fetch = FetchType.LAZY)
	@JoinTable(name = "ROLE_PERMISSION", joinColumns = @JoinColumn(name = "role_id"), inverseJoinColumns = @JoinColumn(name = "permission_id"))
	private Set<PermissionEntity> permissions = new HashSet<PermissionEntity>();

	public String getRoleName() {
		return roleName;
	}

	public void setRoleName(String roleName) {
		this.roleName = roleName;
	}

	public Set<PermissionEntity> getPermissions() {
		return permissions;
	}

	public void setPermissions(Set<PermissionEntity> permissions) {
		this.permissions = permissions;
	}
	
	public Long getParentRoleId() {
		return parentRoleId;
	}

	public void setParentRoleId(Long parentRoleId) {
		this.parentRoleId = parentRoleId;
	}

	@Override
	public String toString() {
		return "["+this.getRoleName() +" : " + this.getId() + "]";
	}
	@Override
	public boolean equals(Object obj) {
		if(obj instanceof RoleEntity){
			if(((RoleEntity)obj).getId()==this.getId() || ((RoleEntity)obj).getRoleName().equalsIgnoreCase(this.getRoleName())){
				return true;
			}
		}
		return false;
	}
	
	@Override
	public int hashCode() {
		return (int) (getId()*7L);
	}
	
	
}
